package com.neutech.util;


import com.neutech.constant.Constant;
import com.sun.istack.internal.NotNull;

import java.sql.*;

public class DBUtil{
    /**
     * 封装连接mysql的CRUD操作 增删改查
     */
    public DBUtil(){}
    /**
     * 连接加载默认数据库
     * @return connection
     */
    public static Connection getConnection(){
        Connection coon = null;
        try{
            // 默认调用下面的有参getConnection函数
            coon = getConnection(Constant.URL,Constant.USERNAME,Constant.PASSWORD);
        }catch (Exception e){
            e.printStackTrace();
        }
        return coon;
    }
    /**
     * 连接加载指定数据库
     */
    public static Connection getConnection(String url,String username,String password){
        Connection coon = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            coon = DriverManager.getConnection(url,username,password);

        }catch (Exception e){
            e.printStackTrace();
        }
        return coon;
    }
    /**
     * 创建statement接口
     */
    public static Statement getstatement(Connection coon){
        Statement stmt = null;
        try {
            stmt = coon.createStatement();
        }catch (Exception e){
            e.printStackTrace();
        }
        return stmt;
    }
    /**
     * 执行sql更新语句
     */
    public static int excuteUpdate(Statement stmt,String sql){
        int rows = 0;
        try {
            rows =  stmt.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }
    /**
     * 执行sql查询语句,返回ResultSet结果
     */
    public static ResultSet getResultSet(Statement stmt,String sql){
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    /**
     * 执行Connection和sql
     * 返回PreparedStatement
     */
    public static PreparedStatement getPreparedStatement(Connection conn, String sql) {
        PreparedStatement prep = null;
        try{
            prep = conn.prepareStatement(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return prep;
    }
    /**
     * PreparedStatement接口执行查询
     * 返回ResultSet结果
     */
    public static ResultSet getResultSet(PreparedStatement prep) {
        ResultSet rs = null;
        try {
            rs = prep.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    /**
     * 关闭接口
     */
    public static void close(@NotNull ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void close(@NotNull Statement stmt){
        try {
            if (stmt != null){
                stmt.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void close( @NotNull Connection coon){
        try {
            if (coon != null){
                coon.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void close(@NotNull Statement stmt, @NotNull Connection coon) {
        close(stmt);
        close(coon);
    }
    public static void close(@NotNull ResultSet rs, @NotNull Statement stmt,
                             @NotNull Connection coon) {
        close(rs);
        close(stmt,coon);
    }
    public static void rollback(Connection conn){
        try {
            conn.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
//    public static void main(String[] args) {
//        System.out.println(getConnection());
//    }
}